package com.pro.ydl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.pro.ydl.service.MainService;
import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.net.RequestListener;

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class TestActivity extends Activity implements OnClickListener , WeiboAuthListener, RequestListener{

	private static final String Tag = "TestActivity";
	private static final String CONSUMER_KEY = "815986801";
	private static final String CONSUMER_SECRET = "9c9e08f115915b03362aba947c65b39c";
	private static final String REDIRECT_URL = "http://www.sina.com.cn";
	ProgressDialog progdialogTest=null;
	private final Handler handler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			if(true == progdialogTest.isShowing())
			{
				progdialogTest.cancel();
			}
			Toast.makeText(TestActivity.this,"加载成功", Toast.LENGTH_SHORT).show();
		}
	};
	public Weibo weibo;
	public Oauth2AccessToken AccessToken;
	Button TestActivityStartServiceButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_activity_layout);
		TestActivityStartServiceButton = (Button) findViewById(R.id.startservicebutton);
		TestActivityStartServiceButton.setOnClickListener(this);
		createProgressBar(this);

	}
	public void closeProgressDailogThread()
	{
		new Thread()
		{
			@Override
			public void run()
			{

					try {
						Thread.sleep(1000*10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				    Message msg = new Message();
				    msg.what = 1;
					handler.sendMessage(msg);
				}

		}.start();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.test_activity_layout, menu);
		return true;
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		int iSrcID = view.getId();
		//Intent intent = new Intent(this, MainService.class);
		switch(iSrcID)
		{
			case R.id.startservicebutton:
			{
				Intent intent = new Intent();
				intent.setClass(TestActivity.this, MainService.class);
				this.startService(intent);
				//progressview();
				//closeProgressDailogThread();
				
			}
			break;
			default:break;
		}
	}

	public void shareweibohandle()
	{
		weibo = Weibo.getInstance(CONSUMER_KEY, REDIRECT_URL);
		weibo.authorize(this, this);


	}
	public void convertviewtobitmapsave()
	{
		View viewConverted = (View)findViewById(R.id.viewtest);
		Bitmap viewBitmap = Bitmap.createBitmap(viewConverted.getWidth(), viewConverted.getHeight(),Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(viewBitmap);
		viewConverted.draw(canvas);


		String sPicturePathName = "/mnt/sdcard/Fish.jpg";
        File f = new File(sPicturePathName);
        if(f.exists())
        {
        	f.delete();
        }
        try {
			f.createNewFile();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        FileOutputStream fOut = null;
        try {
                fOut = new FileOutputStream(f);
        } catch (Exception e) {
                e.printStackTrace();
        }
        viewBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        try {
                fOut.flush();
        } catch (IOException e) {
                e.printStackTrace();
        }
        try {
                fOut.close();
        } catch (IOException e) {
                e.printStackTrace();
        }
	}
	
	public void createProgressBar(Context context)
	{
		ProgressBar progressbarTest = (ProgressBar)findViewById(R.id.progressBar1);
		progressbarTest.setClickable(false);
		progressbarTest.setIndeterminate(true);
		progressbarTest.setMax(100);
		progressbarTest.setProgress(50);
		progressbarTest.setVisibility(ProgressBar.VISIBLE);
	}
	@Override
	public void onCancel() {
		// TODO Auto-generated method stub
		Toast.makeText(this, "授权取消", Toast.LENGTH_LONG).show();
	}

	@Override
	public void onComplete(Bundle arg0) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "授权成功", Toast.LENGTH_LONG).show();
		try
		{
		String Token = arg0.getString("access_token");
		String expires_in = arg0.getString("expires_in");
		this.AccessToken = new Oauth2AccessToken(Token,expires_in);

		if(AccessToken.isSessionValid())
		{
			StatusesAPI API= new StatusesAPI(this.AccessToken);
			//得到当前APP的目录
//			String sPath = this.getFilesDir().getAbsolutePath();
			File fileSD=null;
			String sPicPath = "/mnt/sdcard/Fish.jpg";
			boolean bSDCardSts = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
			if(true == bSDCardSts)
			{
				fileSD = Environment.getExternalStorageDirectory();
			}
			else
			{
				Toast.makeText(this, "没有SD卡", Toast.LENGTH_LONG).show();
			}
			
			if(new File(sPicPath).exists())
			{
				API.upload("测试图片", sPicPath, "0", "0", this);
			}
			else
			{
				Toast.makeText(this, "文件不存在", Toast.LENGTH_LONG).show();
			}
			
		}
		}
		catch(Exception e)
		{
			Toast.makeText(this, "分享异常", Toast.LENGTH_LONG).show();
		}
		
	}

	@Override
	public void onError(WeiboDialogError arg0) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "授权error:"+arg0.getMessage(), Toast.LENGTH_LONG).show();
	}

	@Override
	public void onWeiboException(WeiboException arg0) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "授权异常:"+arg0.getMessage(), Toast.LENGTH_LONG).show();
	}

	@Override
	public void onComplete(String arg0) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "分享成功", Toast.LENGTH_LONG).show();
	}

	@Override
	public void onError(WeiboException arg0) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "分享错误"+arg0.getMessage(), Toast.LENGTH_LONG).show();
	}

	@Override
	public void onIOException(IOException arg0) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "分享异常"+arg0.getMessage(), Toast.LENGTH_LONG).show();
	}
	public void progressview()
	{
		progdialogTest = ProgressDialog.show(TestActivity.this,"正在加载","系统正在处理的请求");

	}
}
